const app = getApp();
Page({
  data: {
    server:app.globalData.server,
    api:app.globalData.api,
    token:'',
    orderList:{
      total:null,
      page: 1,
      list:[],
      show: true,
    },
    dataEnd:false
  },
  onLoad() {
    let that = this;
    my.getStorage({
      key: 'userInfo',
      success: function(res) {
        that.setData({
          token:res.data?res.data.token:'',
          orderList:{
            total:null,
            page: 1,
            list:[],
            show: true,
          },
          dataEnd:false
        })
        that.mySchedulde()
      }
    });
  },
  resetTime(date){
    var date = new Date(date)
    var formatDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' 
				+ (date.getHours()>9?date.getHours():"0"+date.getHours()) 
				+ ':' + (date.getMinutes()>9?date.getMinutes():"0"+date.getMinutes()) 
				+ ':' + (date.getSeconds()>9?date.getSeconds():"0"+date.getSeconds()); 
    return formatDate
  },
  mySchedulde(page = 1) {
    try {
      let url = this.data.api+'/user/myOrder?page='+page;
      let list = this.data.orderList.list;
      let that = this;
      my.request({
        url:url,
        method: 'get',
        headers:{
          'content-type':'application/json',
          'token':this.data.token
        },
        dataType: 'json',
        success: function(res) {
          console.log(res)
          if(res.data.errCode==401){//token过期了
            my.removeStorage({
              key: 'userInfo'
            });
            that.setData({
              token:'',
              orderList:{
                total:null,
                page: 1,
                list:[],
                show: true,
              },
              dataEnd:false
            })
            my.showToast({
              type: 'exception',
              content: res.data.message,
              duration: 2000
            });
            my.reLaunch({ url: '../login/login' });
            return false
          }
          if(res.data.errCode==0){
            let data = res.data.data.result.map((val,index,arr)=>{
              return {
                id: val.id,
                create_time: that.resetTime(val.create_time),
                productName: val.productName,
                productDisc: val.productDisc,
                productPrice: val.productPrice,
                productPic: val.productPic,
                address: val.address,
                num: val.num,
                total_price: val.total_price,
                order_payment_status: val.order_payment_status
              }
            })
            for (let i = 0; i < data.length; i++) {
              list.push(data[i]);
            }
            that.setData({
              orderList:{
                total:res.data.data.total_count,
                page: page,
                list:list,
                show: false
              },
              dataEnd:false
            })
          }
        }
      });
    } catch (e) {
      console.log('mySchedulde执行异常:', e);
    }
  },
  scrollMytrip() {
    console.log(1111111);
    try {
      let page = this.data.orderList.page;
      let list =  this.data.orderList.list;
      // 判断是否还有数据需要加载
      if (list.length < this.data.orderList.total) {
        this.data.orderList.show = true;
        let newPage = page + 1;
        this.mySchedulde(newPage);
      }else{
        this.setData({
          dataEnd:true
        })
      }
    } catch (e) {
      this.data.orderList.show = false;
      console.log('scrollMytrip执行异常:', e);
    }
  },
});
